//
//  TJDiscoverySearchView.swift
//  WeiBo
//
//  Created by TanJian on 15/12/5.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

class TJDiscoverySearchView: UIView,UITextFieldDelegate {



    @IBOutlet weak var textRightTrailing: NSLayoutConstraint!
    @IBOutlet weak var searchField: UITextField!
    
    
    //提供类方法供外界实例化搜索栏
    class func searchView () -> TJDiscoverySearchView {
    
        return NSBundle.mainBundle().loadNibNamed("TJDiscoverySearchView", owner: self, options: nil).last! as! TJDiscoverySearchView
    }
    
    
    override func awakeFromNib() {
        
        //设置左边放大镜图片
        let leftVC = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftVC.frame = CGRectMake(0, 0, frame.height, frame.height)
        leftVC.contentMode = .Center
        
        searchField.leftView = leftVC
        searchField.leftViewMode = UITextFieldViewMode.Always
        //设置text边线宽
        
        searchField.delegate = self
        
        
        
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    

    @IBAction func didClickCancleBtn(sender: UIButton) {
        
        //回退键盘，取消第一响应者
        searchField.resignFirstResponder()
        
        //text填满查询栏
        textRightTrailing.constant = 0
        UIView.animateWithDuration(0.3) { () -> Void in
            
            self.searchField.layoutIfNeeded()
            
        }
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
    
        
        //text填满查询栏
        self.textRightTrailing.constant = 45
        
        UIView.animateWithDuration(0.3) { () -> Void in
            
            self.searchField.layoutIfNeeded()
            
    }
    }
    
}
